package com.youlai.config;

import jakarta.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Map;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryMysql",
        transactionManagerRef = "transactionManagerMysql",
        basePackages =
                {"com.youlai.system.repository",
                "com.youlai.eng.repository",
                "com.youlai.sale.repository",
                        "com.youlai.plan.repository",
                        "com.youlai.purc.repository",
                        "com.youlai.wh.repository",
                        "com.youlai.mrp.repository"})
public class MysqlConfig {

    @Autowired
    @Qualifier("mysqlDataSource")   //换成和DataSourceConfig中对应的名称
    private DataSource dataSource;

    @Autowired
    private HibernateProperties hibernateProperties;

    @Autowired
    private JpaProperties jpaProperties;

    @Bean(name = "entityManagerMysql")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryMysql(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactoryMysql")    //Qmishop实体工厂
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryMysql (EntityManagerFactoryBuilder builder) {
        return builder.dataSource(dataSource)
                .properties(getHibernateProperties())
                .packages("com.youlai.system.entity",
                        "com.youlai.eng.entity",
                        "com.youlai.sale.entity",
                        "com.youlai.plan.entity",
                        "com.youlai.purc.entity",
                        "com.youlai.wh.entity",
                        "com.youlai.mrp.entity")       //换成你自己的实体类所在位置
                .persistenceUnit("MysqlPersistenceUnit")
                .build();
    }

    @Bean(name = "transactionManagerMysql")
    public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryMysql(builder).getObject());
    }

    private Map<String, Object> getHibernateProperties() {
        return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
    }
}


